// pages/sales statistics/sales_statistics.js
var common = require("../../common/common.js").common;

var colorParse = require("../../common/color.js");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose_nav: 0,
        choose_tap: 1,
        page_sign: "sales",
        all: [],
        info: [],
        info2: [],
        per_price: 0,
        total_count: 0,
        clerk: [],
        isIPX: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX
        });
        var that = this;
        that.setData({
            clerk: wx.getStorageSync("clerk")
        });
        this.getBase();
        this.getAll();
        this.getGoods();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    pageSwitch: function(e) {
        wx.showModal({
            title: "提示",
            content: "是否退出团长管理中心",
            success(res) {
                if (res.confirm) {
                    wx.switchTab({
                        url: e.currentTarget.dataset.url
                    });
                }
            }
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长",
                    sleep_on: res.extra.sleep_on
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
                wx.setNavigationBarTitle({
                    title: "旗下" + res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    },
    /**
   * 用户点击右上角分享
   */
    choose_nav: function(e) {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        var id = parseInt(e.currentTarget.dataset.id);
        var choose_tap = that.data.choose_tap;
        if (choose_tap == 1) {
            if (id == 1) {
                common.request({
                    endpoint: "My/myinfo/sale_statistics",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            total_count: res.extra.total_count
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_list",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info: res.extra
                        });
                    }
                });
            } else {
                common.request({
                    endpoint: "My/myinfo/sale_statistics",
                    method: "post",
                    data: {
                        openid: openid,
                        day: id
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            total_count: res.extra.total_count
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_list",
                    method: "post",
                    data: {
                        openid: openid,
                        day: id
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info: res.extra
                        });
                    }
                });
            }
        } else {
            if (id == 1) {
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            per_price: res.extra.per_price
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member_list",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info2: res.extra
                        });
                    }
                });
            } else {
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member",
                    method: "post",
                    data: {
                        openid: openid,
                        day: id
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            per_price: res.extra.per_price
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member_list",
                    method: "post",
                    data: {
                        openid: openid,
                        day: id
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info2: res.extra
                        });
                    }
                });
            }
        }
        this.setData({
            choose_nav: id
        });
    },
    choose_tap: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var choose_nav = that.data.choose_nav;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        if (id == 1) {
            if (choose_nav == 1) {
                common.request({
                    endpoint: "My/myinfo/sale_statistics",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            total_count: res.extra.total_count
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_list",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info: res.extra
                        });
                    }
                });
            } else {
                common.request({
                    endpoint: "My/myinfo/sale_statistics",
                    method: "post",
                    data: {
                        openid: openid,
                        day: choose_nav
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            total_count: res.extra.total_count
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_list",
                    method: "post",
                    data: {
                        openid: openid,
                        day: choose_nav
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info: res.extra
                        });
                    }
                });
            }
        } else {
            if (choose_nav == 1) {
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            per_price: res.extra.per_price
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member_list",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info2: res.extra
                        });
                    }
                });
            } else {
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member",
                    method: "post",
                    data: {
                        openid: openid,
                        day: choose_nav
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            all: res.extra,
                            per_price: res.extra.per_price
                        });
                    }
                });
                common.request({
                    endpoint: "My/myinfo/sale_statistics_member_list",
                    method: "post",
                    data: {
                        openid: openid,
                        day: choose_nav
                    },
                    header: {
                        "content-type": "application/json"
                    },
                    success: res => {
                        that.setData({
                            info2: res.extra
                        });
                    }
                });
            }
        }
        this.setData({
            choose_tap: id
        });
    },
    scancode: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        wx.scanCode({
            success: res => {
                wx.navigateTo({
                    url: "/" + res.path,
                    success() {},
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: "请扫描正确的提货二维码"
                        });
                    }
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: "请扫描正确的提货二维码",
                    showCancel: false
                });
            }
        });
    },
    getAll: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/myinfo/sale_statistics",
            method: "post",
            data: {
                openid: openid,
                day: 0
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    all: res.extra,
                    total_count: res.extra.total_count
                });
            }
        });
    },
    getGoods: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.request({
            endpoint: "My/myinfo/sale_statistics_list",
            method: "post",
            data: {
                openid: openid,
                day: 0
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
            }
        });
    }
});